//
//  HMNewFeatureViewController.swift
//  Faner_Weibo
//
//  Created by 范英强 on 16/1/30.
//  Copyright © 2016年 范英强. All rights reserved.
//

import UIKit

class HMNewFeatureViewController: UIViewController,UIScrollViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    private func setupUI(){
        scrollView.frame = view.bounds
        view.addSubview(scrollView)
        pageControl.frame = CGRect(x: scrollView.frame.width * 0.5, y: scrollView.frame.height - 100, width: 0, height: 0)
        view.addSubview(pageControl)
        
        let count = 4
        for i in 0..<count {
            let imageView = UIImageView(image: UIImage(named: "new_feature_\(i + 1)"))
            imageView.frame = CGRect(x: scrollView.frame.width * CGFloat(i), y: 0, width: scrollView.frame.width, height: scrollView.frame.height)
            scrollView.addSubview(imageView)
            if i == count - 1{
                addLastPage(imageView)
            }
        }
        scrollView.contentSize = CGSize(width: scrollView.frame.width * CGFloat(count), height: 0)
        scrollView.delegate = self
        pageControl.numberOfPages = count
        
    }
    
    private func addLastPage(page: UIView){
        page.userInteractionEnabled = true
        enterButton.center = CGPointMake(page.frame.width * 0.5, page.frame.height - 150)
        page.addSubview(enterButton)
        
        sharedButton.center = CGPoint(x: enterButton.center.x, y: CGRectGetMinY(enterButton.frame) - 30)
        page.addSubview(sharedButton)
    }
    // MARK: - scrollView delegate
    func scrollViewDidScroll(scrollView: UIScrollView) {
        let contentOffSetX = scrollView.contentOffset.x
        
        let index = contentOffSetX / scrollView.frame.width
        
        pageControl.currentPage = Int(index + 0.5)
        
        
    }
    
    // MARK: - 点击事件
    
    @objc private func sharedButtonClick(button: UIButton) {
        button.selected = !button.selected
    }
    
    @objc private func enterButtonClick(){
        print("进入")
    }
    
    // MARK: - 懒加载控件
    private lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.bounces = false
        scrollView.pagingEnabled = true
        return scrollView
    }()
    
    private lazy var pageControl: UIPageControl = {
        let pageControl = UIPageControl()
        pageControl.currentPageIndicatorTintColor = UIColor.orangeColor()
        pageControl.pageIndicatorTintColor = UIColor.blueColor()
        return pageControl
    }()
    
    private lazy var enterButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "enterButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        button.setTitle("进入微博", forState: UIControlState.Normal)
        button.sizeToFit()
        return button
    }()
    
    private lazy var sharedButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "sharedButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        button.setImage(UIImage(named: "new_feature_share_false"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "new_feature_share_true"), forState: UIControlState.Selected)
        
        button.setTitle(" 分享到微博", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        button.sizeToFit()
        return button
        
    }()

}
